home *** CD-ROM | disk | FTP | other *** search
/ Graphics Plus / Graphics Plus.iso / general / modelers / geomview / source.lha / Geomview / src / lib / mib / mibMenu.c < prev    next >
Encoding:
C/C++ Source or Header  |  1993-09-17  |  6.3 KB  |  237 lines

  1. #include "mibload.h"
  2. #include "mibwidgets.h"
  3.  
  4. extern Display    *dpy;
  5. extern GC    mib_gc;
  6. static int    delhandler; /* delay adding event handler until menu
  7.                     is actually created */
  8.  
  9. /* Code for Menu */
  10. /*****************************************************************************/
  11.  
  12. mib_Widget *mib_create_Menu(mib_Widget *parent, char *name, char *label,
  13.         int posx, int posy, int width, int height, int mib_fill)
  14. {
  15.   mib_Widget    *temp;
  16.   mib_Menu    *myres;
  17.   Widget     wtemp;
  18.   char         label_temp[50];
  19.   Arg         args[20];
  20.   int         n;
  21.  
  22.   /* create the new widget and add it to the tree */
  23.  
  24.   temp = mib_new_mib_Widget();
  25.   if (mib_fill == WDEFAULT)
  26.     mib_add_backward(temp, parent);
  27.   else
  28.     mib_add_mib_Widget(temp, parent);
  29.  
  30.   myres = (mib_Menu *)malloc(sizeof(mib_Menu));
  31.  
  32.   /* initialize public resources */
  33.  
  34.   if (mib_fill == WDEFAULT)
  35.   {
  36.     temp->name = (char *)malloc(strlen(name)+1);
  37.     strcpy(temp->name,name);
  38.   }
  39.   temp->mib_class = (char *)malloc(8);
  40.   sprintf(temp->mib_class,"Menu");
  41.   temp->mib_class_num = MIB_MENU;
  42.   temp->width = width;
  43.   temp->height = height;
  44.   temp->topOffset = posy;
  45.   temp->leftOffset = posx;
  46.   temp->bottomOffset = 0;
  47.   temp->rightOffset = 0;
  48.   temp->topAttachment = 1;
  49.   temp->leftAttachment = 1;
  50.   temp->bottomAttachment = 0;
  51.   temp->rightAttachment = 0;
  52.  
  53.   temp->mib_allowresize = 0;
  54.  
  55.   /* initialize private resources */
  56.  
  57.   temp->myres = (void *)myres;
  58.   myres->numitems = 0;
  59.  
  60.   /* create Xt widget */
  61.  
  62.   n = 0;
  63.  
  64.   if (mib_fill == WDEFAULT)
  65.   {
  66.     XtSetArg (args[n], XmNleftAttachment, XmATTACH_FORM); n++;
  67.     XtSetArg (args[n], XmNleftOffset, posx);n++;
  68.     XtSetArg (args[n], XmNtopAttachment, XmATTACH_FORM); n++;
  69.     XtSetArg (args[n], XmNtopOffset, posy);n++;
  70.   }
  71.   XtSetArg (args[n], XmNrubberPositioning, False); n++;
  72.  
  73.   temp->me = XtCreateManagedWidget(name, xmRowColumnWidgetClass,
  74.                 temp->parent->me, args, n);
  75.  
  76.   n = 0;
  77.  
  78.   delhandler = 1;
  79.   if (mib_fill == WDEFAULT)
  80.   {
  81.     delhandler = 0;
  82.     myres->numitems = 3;
  83.     myres->my_menu = (MenuItem *)malloc(sizeof(MenuItem) * (myres->numitems+1));
  84.     myres->items = (Widget *)malloc(sizeof(Widget) * myres->numitems);
  85.     sprintf(label_temp, "Item 1");
  86.     myres->my_menu[0].label = (char *)malloc(strlen(label_temp)+1);
  87.     strcpy(myres->my_menu[0].label,label_temp);
  88.     myres->my_menu[0].class = &xmPushButtonGadgetClass;
  89.     myres->my_menu[0].mnemonic = '1';
  90.     myres->my_menu[0].accelerator = NULL;
  91.     myres->my_menu[0].accel_text = NULL;
  92.     myres->my_menu[0].accel_text = NULL;
  93.     myres->my_menu[0].callback = NULL;
  94.     myres->my_menu[0].callback_data = NULL;
  95.     myres->my_menu[0].subitems = (struct _menu_item *)1;
  96.  
  97.     sprintf(label_temp, "Item 2");
  98.     myres->my_menu[1].label = (char *)malloc(strlen(label_temp)+1);
  99.     strcpy(myres->my_menu[1].label,label_temp);
  100.     myres->my_menu[1].class = &xmPushButtonGadgetClass;
  101.     myres->my_menu[1].mnemonic = '2';
  102.     myres->my_menu[1].accelerator = NULL;
  103.     myres->my_menu[1].accel_text = NULL;
  104.     myres->my_menu[1].accel_text = NULL;
  105.     myres->my_menu[1].callback = NULL;
  106.     myres->my_menu[1].callback_data = NULL;
  107.     myres->my_menu[1].subitems = (struct _menu_item *)1;
  108.  
  109.     sprintf(label_temp, "Item 3");
  110.     myres->my_menu[2].label = (char *)malloc(strlen(label_temp)+1);
  111.     strcpy(myres->my_menu[2].label,label_temp);
  112.     myres->my_menu[2].class = &xmPushButtonGadgetClass;
  113.     myres->my_menu[2].mnemonic = '2';
  114.     myres->my_menu[2].accelerator = NULL;
  115.     myres->my_menu[2].accel_text = NULL;
  116.     myres->my_menu[2].accel_text = NULL;
  117.     myres->my_menu[2].callback = NULL;
  118.     myres->my_menu[2].callback_data = NULL;
  119.     myres->my_menu[2].subitems = (struct _menu_item *)1;
  120.  
  121.     myres->my_menu[3].label = NULL;
  122.  
  123.     wtemp = BuildMenu(temp->me, XmMENU_OPTION, NULL, 'M', myres->my_menu);
  124.     XtManageChild(wtemp);
  125.   }
  126.  
  127.   if (mib_fill == WEDIT || mib_fill == WDEFAULT)
  128.   {
  129.  
  130.     /* default menu */
  131.  
  132.     if (!delhandler)
  133.     {
  134.       mib_apply_eventhandlers(wtemp, temp);
  135.       mib_apply_eventhandlers(temp->me, temp);
  136.     }
  137.   }
  138.  
  139.   if (mib_fill == WEMPTY)
  140.     delhandler = 0;
  141.  
  142.   return temp;
  143. }
  144.  
  145. void mib_delete_Menu(mib_Widget *this)
  146. {
  147.   mib_Menu *temp = (mib_Menu *)this->myres;
  148.  
  149.   free(temp);
  150. }
  151.  
  152. void mib_save_Menu(mib_Widget *this, FILE *fout)
  153. {
  154.   mib_Menu *temp = (mib_Menu *)this->myres;
  155.   int count;
  156.  
  157.   fprintf(fout, "numitems: %d\\n\\\n", temp->numitems);
  158.  
  159.   for (count=0; count < temp->numitems; count++)
  160.   {
  161.     fprintf(fout, "item: \\\"%s\\\"\\n\\\n", temp->my_menu[count].label);
  162.   }
  163.  
  164. }
  165.  
  166. int mib_load_Menu(mib_Widget *this, mib_Buffer *fin)
  167. {
  168.   char          res[MI_MAXSTRLEN];
  169.   char          val[MI_MAXSTRLEN];
  170.   int        count, vallen;
  171.   Widget    wtemp;
  172.   mib_Menu     *myres = (mib_Menu *)this->myres;
  173.   
  174.  
  175.   if (!mib_read_line(fin, res, val))
  176.     return 0;
  177.  
  178.   if (!strcmp(res, "numitems"))
  179.   {
  180.     myres->numitems = 0;
  181.     sscanf(val, "%d", &(myres->numitems));
  182.     if (!(myres->numitems))
  183.       return 0;
  184.  
  185.     myres->my_menu = (MenuItem *)malloc(sizeof(MenuItem) * (myres->numitems+1));
  186.     myres->items = (Widget *)malloc(sizeof(Widget) * myres->numitems);
  187.     for (count = 0; count < myres->numitems; count++)
  188.     {
  189.       if (!mib_read_line(fin, res, val))
  190.     return 0;
  191.       vallen = strlen(val);
  192.       if (vallen < 2)
  193.     return 0;
  194.       val[vallen-1] = '\0';
  195.       myres->my_menu[count].label = (char *)malloc(vallen-1);
  196.       sprintf(myres->my_menu[count].label, "%s", &(val[1]));
  197.       myres->my_menu[count].class = &xmPushButtonGadgetClass;
  198.       myres->my_menu[count].mnemonic = NULL;
  199.       myres->my_menu[count].accelerator = NULL;
  200.       myres->my_menu[count].accel_text = NULL;
  201.       myres->my_menu[count].accel_text = NULL;
  202.       myres->my_menu[count].callback = NULL;
  203.       myres->my_menu[count].callback_data = NULL;
  204.       myres->my_menu[count].subitems = (struct _menu_item *)1;
  205.     }
  206.     myres->my_menu[myres->numitems].label = NULL;
  207.  
  208.     wtemp = BuildMenu(this->me, XmMENU_OPTION, NULL, NULL, myres->my_menu);
  209.     XtManageChild(wtemp);
  210.  
  211.     if (delhandler)
  212.     {
  213.       mib_apply_eventhandlers(wtemp, this);
  214.       mib_apply_eventhandlers(this->me, this);
  215.     }
  216.     else
  217.       this->me = wtemp;
  218.  
  219.     for (count = 0; count < myres->numitems; count++)
  220.     {
  221.       /* this hack extracts the widget for each item in the menu
  222.      for possible use by the programmer */
  223.       myres->items[count] = (Widget)(myres->my_menu[count].subitems);
  224.     }
  225.       
  226.   }
  227.   else
  228.     return 0;
  229.  
  230.   if (!mib_read_line(fin, res, val))
  231.     return 0;
  232.   if (strcmp(res,"EndWidget"))
  233.     return 0;
  234.  
  235.   return 1;
  236. }
  237.